package spring.cloud.tasks.executor.utils;


import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Setter
@Getter
public class ThreadFactory implements java.util.concurrent.ThreadFactory {
    private static final AtomicInteger poolNumber = new AtomicInteger(1);
    private AtomicInteger threadNumber = new AtomicInteger(1);
    private boolean isMultiple = true;
    private String threadName;

    public ThreadFactory(String threadName) {
        this.threadName = threadName + "-" + poolNumber.getAndIncrement() + "-thread-";
    }

    public ThreadFactory(String threadName, boolean isMultiple) {
        this.isMultiple = isMultiple;
        this.threadName = threadName;
    }

    @Override
    public Thread newThread(Runnable runnable) {
        String name = isMultiple ? threadName + threadNumber.getAndIncrement() : threadName;
        Thread thread = new Thread(runnable, name);
        thread.setDaemon(false);
        thread.setPriority(Thread.NORM_PRIORITY);
        return thread;
    }
}
